﻿using System;
using System.Reflection;

namespace TC
{
    public class ValueInputWithEvent<T> : ValueInput<T>
    {
        public ValueInputWithEvent(FlowNode parent, string name) : base(parent, name)
        {
        }

        public override bool CanConnect(Port other)
        {
            if (other.GetType().IsSubclassOf(typeof(ValueOutputWithEvent<>)) && !other.IsInput && type.IsAssignableFrom(other.type))
            {
                return true;
            }
            return false;
        }

        public override bool CanConnect(Type nodeType, out FieldInfo portInfo)
        {
            var fields = nodeType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var field in fields)
            {
                if (field.FieldType == typeof(ValueOutputWithEvent<T>))
                {
                    portInfo = field;
                    return true;
                }
            }
            portInfo = default;
            return false;
        }
    }
}
